package com.hzdl.car.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Customer;
import com.hzdl.car.service.CustomerService;
import com.hzdl.car.util.AJAXResult;
import com.hzdl.car.util.CustomExcelUtil;
import com.hzdl.car.vo.CustomerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService cs;

    @RequestMapping("/cusInit")
    public String cusInit(){
        return "/system/customer";
    }

    @RequestMapping("/customerShow")
    @ResponseBody
    public AJAXResult customerShow(CustomerVo customerVo){
        if(customerVo.getPage()==null){
            customerVo.setPage(1);
        }
        if(customerVo.getLimit()==null){
            customerVo.setLimit(5);
        }
        PageInfo pageInfo=cs.findCustomerByWhere(customerVo);
        return new AJAXResult(0,"",pageInfo.getList(),pageInfo.getTotal());
    }
   @RequestMapping("/addCustomer")
    @ResponseBody
    public AJAXResult addCustomer(CustomerVo customerVo){
            int row=cs.insertCustomer(customerVo);
            if(row==0){
                return AJAXResult.ADD_ERROR;
            }
            return AJAXResult.ADD_SUCCESS;
        }

   @RequestMapping("/updateCustomer")
   @ResponseBody
   public AJAXResult updateCustomer(CustomerVo customerVo){
       int row1=cs.updateCustomer(customerVo);
       if(row1==0){
           return AJAXResult.UPDATE_ERROR;
       }
       return AJAXResult.UPDATE_SUCCESS;
   }
   @RequestMapping("/customerDelete")
    @ResponseBody
    public AJAXResult customerDelete(String identity){
        int row=cs.deleteCustomerByIdentity(identity);
        if(row==0){
            return AJAXResult.DELETE_ERROR;
        }
        return AJAXResult.DELETE_SUCCESS;
   }
   @RequestMapping("/deleteAllCustomer")
    @ResponseBody
    public AJAXResult deleteAllCustomer(String ids){
        String[] identity =ids.split(",");
        int row=0;
        for(int i=0;i<identity.length;i++){
            row+=cs.deleteCustomerByIdentity(identity[i]);
        }
        if(row==0){
            return AJAXResult.DELETE_ERROR;
        }
        return AJAXResult.DELETE_SUCCESS;
   }

   @RequestMapping("/customerExport")
   @ResponseBody
    public ResponseEntity<Object> customerExport (CustomerVo customerVo){
        List<Customer> cList =cs.exportCustomer(customerVo);
        ByteArrayOutputStream stream = CustomExcelUtil.create2(cList);
        String fileName = null;
        //导出文件名称
       try {
           fileName = URLEncoder.encode("客户信息.xls","UTF-8");
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       }
           //设置下载是的头部响应对象
           HttpHeaders httpHeader = new HttpHeaders();
           httpHeader.setContentType(MediaType.APPLICATION_OCTET_STREAM);
           httpHeader.setContentDispositionFormData("attachment",fileName);

       return new ResponseEntity<Object>(stream.toByteArray(),httpHeader, HttpStatus.CREATED);
   }
}
